// pages/gx/gx.js
var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items:[{},{},{}],
		selected: true,  //tab选项卡控制器
		selected1: false,
    checked:false,
    selectAllStatus:false,
    checknum:false,
    checknum1:false,
    arr:[],
    arr1:[]
	},
	selected: function (e) {
    this.onShow()
		this.setData({
			selected1: false,
			selected: true
		})
	},
	selected1: function (e) {
    this.onShow()
		this.setData({
			selected: false,
			selected1: true
		})
	},


  checkboxChange:function(e){
    console.log(e)
    var index = e.currentTarget.dataset.index;
    var value=e.detail.value[0]
    
    if (value){
      this.setData({
        checked:true,
      
      })
    }else{
      this.setData({
        checked: false,
       
      })
    }
   let checked=this.data.checked;
    let carts = this.data.arr;
    carts[index].checked = checked
    this.setData({
      checked: checked,
      arr: carts
    });
    console.log(this.data.arr)
  },

  


  quanxuan:function(e){
    let selectAllStatus = this.data.selectAllStatus;    // 是否全选状态
    
    selectAllStatus = !selectAllStatus;
    let carts = this.data.arr;
    for (let i = 0; i < carts.length; i++) {
      carts[i].checked = selectAllStatus;            // 改变所有商品状态
    }
    this.setData({
    
      selectAllStatus: selectAllStatus,
      arr: carts
    });
    console.log(this.data.arr)
   
  },

  rbtn:function(){
    let carts = this.data.arr;
    var that=this;
    var wx_id=wx.getStorageSync('uid');
    
    

   
    // for (let i = carts.length - 1; i >= 0; i--) {
    //   if (carts[i].checked == true) {
    //     that.setData({
    //       checknum: true
    //     })
    //   }else{
    //     that.setData({
    //       checknum: false
    //     })
    //   }
    // }
    
    console.log(that.data.checknum)

   
      wx.showModal({
        title: '提示',
        content: '确定删除？',
        success: function (res) {
          if (res.confirm) {
            for (let i = carts.length - 1; i >= 0; i--) {
              if (carts[i].checked == true) {
                var id = carts[i].info_id;
                var info_type = carts[i].info_type;
                wx.request({
                  url: app.globalData.url +'xcx/delresource',
                  data: {
                    wx_id: wx_id,
                    info_id: id,
                    info_type: info_type
                  },
                  method: "post",
                  success: function (res) {
                    console.log(res)
                  }
                })


                carts.splice(i, 1);
                console.log(id)

              } else {
                // wx.showToast({
                //   title: '请选择要删除的供需',
                //   icon: 'none',
                //   duration: 1000
                // })

              }
            }
            that.setData({
              arr: carts,
              arr1:[]
            });

          } else if (res.cancel) {

          }
        }
      })


  
   
   
  },


  checkboxChange1: function (e) {
    console.log(e)
    var index = e.currentTarget.dataset.index;
    var value = e.detail.value[0]

    if (value) {
      this.setData({
        checked: true,
      
      })
    } else {
      this.setData({
        checked: false,
       
      })
    }
    let checked = this.data.checked;
    let carts = this.data.arr1;
    carts[index].checked = checked
    this.setData({
      checked: checked,
      arr1: carts
    });
    console.log(this.data.arr1)
  },


  quanxuan1: function (e) {
    let selectAllStatus = this.data.selectAllStatus;    // 是否全选状态
   
    selectAllStatus = !selectAllStatus;
   
    let carts = this.data.arr1;
    for (let i = 0; i < carts.length; i++) {
      carts[i].checked = selectAllStatus;            // 改变所有商品状态
    }
    this.setData({
     
      selectAllStatus: selectAllStatus,
      arr1: carts
    });
    console.log(this.data.arr1)

  },

  
  rbtn1: function () {
    let carts = this.data.arr1;
    var that = this;
    var wx_id = wx.getStorageSync('uid');
   

    // for (let i = carts.length - 1; i >= 0; i--) {
    //   if (carts[i].checked == true) {
    //     that.setData({
    //       checknum1: true
    //     })
    //   }else{
    //     that.setData({
    //       checknum1: false
    //     })
    //   }
    // }
  
   
      wx.showModal({
        title: '提示',
        content: '确定删除？',
        success: function (res) {
          if (res.confirm) {
            for (let i = carts.length - 1; i >= 0; i--) {
              if (carts[i].checked == true) {
                var id = carts[i].info_id;
                var info_type = carts[i].info_type;
                wx.request({
                  url: app.globalData.url +'xcx/delresource',
                  data: {
                    wx_id: wx_id,
                    info_id: id,
                    info_type: info_type
                  },
                  method: "post",
                  success: function (res) {
                    console.log(res)
                  }
                })


                carts.splice(i, 1);
                console.log(id)

              } else {
                // wx.showToast({
                //   title: '请选择要删除的供需',
                //   icon: 'none',
                //   duration: 1000
                // })

              }
            }
            that.setData({
              arr1: carts,
              arr:[]
            });

          } else if (res.cancel) {

          }
        }
      })


   


    

  },



  // checknum1: function () {
  //   let carts = this.data.arr1;
  //   var that = this;
  //   for (let i = 0; i < carts.length; i++) {
  //     if (carts[i].checked == true) {
  //       var a = 1;
       
  //     }
  //   }
  //   that.setData({
  //     checknum1: a
  //   })

  // },




  fabu:function(){
    let carts = this.data.arr;
    var that = this;
    var wx_id = wx.getStorageSync('uid');

    

   
      wx.showModal({
        title: '提示',
        content: '确定发布？',
        success: function (res) {
          if (res.confirm && carts) {
            for (let i = carts.length - 1; i >= 0; i--) {
              if (carts[i].checked == true) {
                var id = carts[i].info_id;
                wx.request({
                  url: app.globalData.url +'xcx/showmarket',
                  data: {
                    info_id: id
                  },
                  method: "post",
                  success: function (e) {
                    if (e.data.code == 10000) {
                      wx.redirectTo({
                        url: '../gx/gx',
                      })
                    }
                    console.log(e)
                  }
                })


              }else{
                // wx.showToast({
                //   title: '请选择要发布的供需',
                //   icon: 'none',
                //   duration: 1000
                // })


              }



            }


          }

        }

      })

 
    
  },



  shuaxin:function(){
    var wx_id = wx.getStorageSync('uid')
    var that = this
    wx.request({
      url: app.globalData.url +'xcx/allresource/' + wx_id,
      method: 'get',
      success: function (res) {
        console.log(res)
        if(res.data.data){
          that.setData({
            arr: res.data.data
          })
        }
        
        wx.showToast({
          title: '刷新成功',
        })
      }
    })

    wx.request({
      url: app.globalData.url +'xcx/ShowResoures/' + wx_id,
      method: 'get',
      success: function (res) {
        console.log(res)
        if(res.data.data){
          that.setData({
            arr1: res.data.data
          })
        }
       
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    var wx_id = wx.getStorageSync('uid')
    var that = this
    wx.request({
      url: app.globalData.url + 'xcx/allresource/' + wx_id,
      method: 'get',
      success: function (res) {
        console.log(res)
        if (res.data.data) {
          that.setData({
            arr: res.data.data
          })
        }

      }
    })

    wx.request({
      url: app.globalData.url + 'xcx/ShowResoures/' + wx_id,
      method: 'get',
      success: function (res) {
        console.log(res)
        if (res.data.data) {
          that.setData({
            arr1: res.data.data
          })
        }

      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  add:function(){
    wx.navigateTo({
        url: '../bj/bj',
      })
  }
})